﻿/********************************************************************
	日期:2022/4/28 21:20
	名称:LoginSys
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:登录服务
*********************************************************************/

using System;
using PEProtocol;

public class LoginSys{
    private static LoginSys instance = null;

    public static LoginSys Instance{
        get{
            if (instance == null){
                instance = new LoginSys();
            }

            return instance;
        }
    }

    private CacheSvc cacheSvc = null;
    private TimerSvc timerSvc = null;

    public void Init(){
        cacheSvc = CacheSvc.Instance;
        timerSvc = TimerSvc.Instance;
        PECommon.Log("LoginSys Init done");
    }

    public void ReqLogin(MsgPack pkt){
        ReqLogin data = pkt.msg.reqLogin;
        // TODO
        //当前账号是否已经上线
        GameMsg msg = new GameMsg{
            cmd = (int) CMD.ResLogin
        };

        if (cacheSvc.IsAcctOnLine(data.acct)){
            //已上线：返回错误信息
            msg.err = (int) ErrorCode.AcctIsOnLine;
        }
        else{
            //未上线：
            PlayerData pd = cacheSvc.GetPlayerData(data.acct, data.pass);
            //当前账号是否存在
            if (pd == null){
                //存在，密码错误
                msg.err = (int) ErrorCode.WrongPass;
            }
            else{
                //计算离线体力增长
                int power = pd.power;
                long now = timerSvc.GetNowTime();
                long ms = now - pd.time;
                int addPower = (int) (ms / (1000 * 60 * PECommon.PowerAddSpace)) * PECommon.PowerAddCount;
                if (addPower > 0){
                    int powerMax = PECommon.GetPowerLimit(pd.level);
                    if (pd.power < powerMax){
                        pd.power += addPower;
                        if (pd.power > powerMax){
                            pd.power = powerMax;
                        }
                    }
                }

                if (power != pd.power){
                    cacheSvc.UpdatePlayerData(pd.id, pd);
                }
                
                msg.resLogin = new ResLogin{
                    playerData = pd
                };
                cacheSvc.AcctOnLine(data.acct, pkt.session, pd);
            }
        }

        //回应客户端
        pkt.session.SendMsg(msg);
    }

    public void ReqRename(MsgPack pkt){
        ReqRename data = pkt.msg.reqRename;
        GameMsg msg = new GameMsg{
            cmd = (int) CMD.ResRename
        };

        if (cacheSvc.IsNameExists(data.name)){
            //存在返回错误码
            msg.err = (int) ErrorCode.NameIsExists;
        }
        else{
            //不存在更新缓存以及数据库，再返回给客户端
            PlayerData playerData = cacheSvc.GetPlayerDataBySession(pkt.session);
            playerData.name = data.name;
            if (!cacheSvc.UpdatePlayerData(playerData.id, playerData)){
                msg.err = (int) ErrorCode.UpdateDBError;
            }
            else{
                msg.resRename = new ResRename{
                    name = data.name
                };
            }
        }

        pkt.session.SendMsg(msg);
    }

    public void ClearOffineData(ServerSession session){
        //写入下线时间
        PlayerData pd = cacheSvc.GetPlayerDataBySession(session);
        if (pd != null){
            pd.time = timerSvc.GetNowTime();
            if (!cacheSvc.UpdatePlayerData(pd.id, pd)){
                PECommon.Log("Update offline time error", LogType.Error);
            }
        }
        cacheSvc.AcctOffine(session);
    }
}